<script>
import {defineComponent} from "vue";
import router from "@/router";

export default defineComponent({
  name: 'cart',
  data () {
    return {
      cartlist: this.$store.getters['getcartList']
    }
  },
  computed: {
    totalprice: {
      get () {
        let total = 0
        for (let cart of this.cartlist) {
            total += cart.Price * cart.Count
        }
        return total
      }
    }
  },
  methods: {
    del (index) {
        this.cartlist = this.cartlist.filter(item => item.id !== index);
        this.$store.commit('setcartList', this.cartlist);
      this.$store.commit('setcartList', this.cartlist);
    },
    settle(e){  
        e.preventDefault();
        let total = 0
        for (let cart of this.cartlist) {
            total += cart.Price * cart.Count
        }
        // 点击按钮将总价保存
        this.$store.dispatch('setTotalPrice', total)
        .then(()=>{
            console.log("!!!");
            router.push('/index/new-order')
            })
            .catch((error)=>{
             console.error('异常',error)
            })
    },
    returnToMain(){
        this.$router.push({name:'main'})
    },
  }
})
</script>

<template>
    <div id="BackLink">
        <a href="#" @click="returnToMain()">Return to Main Menu</a>
    </div>

    <div class="crumb">
        <div class="w">
            <div class="crumb-content">
                <a href="" class="link">MyStore</a>
                <span>></span>
                <span class="link-text">My Cart</span>
            </div>
        </div>
    </div>

    <div class="page-wrap w">
        <div class="panel">
            <div class="panel-title">Shopping Cart</div>
                <div class="panel-body">
                    <table class="product-table">
                        <tr>
                            <th class="cell-img">Product Picture</th>
                            <th class="cell-id">Product ID</th>
                            <th class="cell-info">Category</th>
                            <th class="cell-price">Price</th>
                            <th class="cell-count">Quantity</th>
                            <th class="cell-total">Total price</th>
                            <th class="cell-operator">Operate</th>
                        </tr>
                            <tr v-for="item in cartlist" :key="item.id">
                                <td class="cell-img">
                                    <div class="link">
                                        <!-- <img class="product-img" src="" alt=""> -->
                                        <img :src="'data:image/png;base64,'+item.Item_Picture"/>
                                    </div>
                                </td>
                                <td class="cell-id">
                                    {{ item.Item_Id }}
                                </td>
                                <td class="cell-info">
                                    <a href="#" class="link">
                                        {{ item.Description }}
                                    </a>
                                </td>
                                <td class="cell-price">
                                    <div class="single-price">
                                        ${{ item.Price }}
                                    </div>
                                </td>
                                <td class="cell-count">
                                    <button class="op minus" @click="item.Count--" :disabled="item.Count<=1">-</button>
                                        <div class="count-num">{{ item.Count }}</div>
                                    <button class="op add" @click="item.Count++">+</button>
                                </td>
                                <td class="cell-total">
                                    {{item.Price * item.Count}}
                                </td>
                                <td class="cell-operator">
                                    <a href="#" @click.prevent="del(item.id)">Delete</a>
                                </td>
                            </tr>
                    </table>
                    <div class="submit-content">
                        <span>sum:</span>
                        <span class="total-price">
                            {{totalprice.toFixed(2)}}
                        </span>
                        <a href="" class="btn order-submit" @click="settle">settle account</a>
                    </div>
                    <!-- 该句有触发条件 -->
                    <!-- <h2 id="information" style="text-align:center">Your cart is empty!</h2> -->
                </div>
        </div>
    </div>
</template>

<style scoped>
@import url("http://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap"); /* CSS Reset */
*{
    margin: 0;
    padding: 0;
}

body{
    background: #f6f6f6;
    /* 字体大小可以放大一点 */
     font: 14px/1.5 tahoma, Helvetica, arial, MicroSoft YaHei, sans-serif;
    /*font: 14px/1.5 'Muli', sans-serif;*/
    width: 1520px;
}

li{
    list-style: none;
}

img{
    margin: 0 auto;
    border: none;
}

label{
    cursor: pointer;
}

/* 定宽布局 */
.w{
    width: 1080px;
    margin: 0 auto;
    position: relative;
    overflow: hidden;
    /*清除浮动，查资料*/
}

.link{
    color: #999;
    cursor: pointer;
    text-decoration: none;
}

.link:hover{
    color: #215496;
}

.link-text{
    color: #999;
    text-decoration: none;
}

.btn{
    display: inline-block;
    padding: 0 20px;
    height: 40px;
    line-height: 40px;
    vertical-align: middle;
    border: none;
    background: #215496;
    color: #fff;
}

.nav-top .user-info .link-text{
    margin-right: 5px;
}

/* 面包屑导航 */
.crumb{
    height: 50px;
    line-height: 50px;
    background: #eee;
    color: #888;
    font-size: 14px;
    border-top: 2px solid #215496;
    margin-bottom: 10px;
}

#Sidebar {
    float: left;
    width: 30%;
}

#MainImage {
    float: right;
    text-align:center;
    width: 50%;
}

#productAutoComplete{
    background: #ccc;
    position: absolute;
    top: 134px;
    left: 470px;
    width: 624px;
    z-index: 999;
    display: none;
}

#productAutoList{
    margin: 0;
    padding: 0;
    border: 1px solid #aaa;
}

.productAutoItem{
    list-style: none;
    height: 30px;
    line-height: 28px;
    font-size: 25px;
    text-align: left;
    padding-left: 3px;
    margin-top: 3px;
    border-bottom: 1px solid #666;
    cursor: pointer;
}

/* 引入网络字体（Poppins） */

#ShowBox{
  border: 5px solid #000000;
  border-radius: 10px;
  margin: 0;
  float: right;
  width: 70%;
  box-sizing: border-box;
  font-family: "Poppins";
  display: none;
}

#back{
    position: relative;
    padding: 8px 20px;
    margin-top: 5px;
    background-color: #000;
    color: #fff;
    border-radius: 20px;
    text-decoration: none;
    font-weight: 500;
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    margin-left: 220px;
    width: 145px;
}
    /* 页面panel */
.panel{
    padding: 10px;
    background: #fff;
    margin-bottom: 10px;
}

.panel .panel-title{
    padding: 10px;
    color: #666;
    font-size: 14px;
    font-weight: bold;
    border-bottom: 1px solid #eee;
}

.panel .panel-body{
    padding: 10px;
    /* 消除浮动，防止东西过多 */
    overflow: hidden;
}



/* 商品清单 */
.product-table{
    width: 100%;
    margin: 10px 0;
    border-collapse: collapse;
}

.product-table th,td{
    border-bottom: 1px dotted #ddd;
}

.product-table .cell-img{
    width: 15%;
    padding: 10px;
    text-align: center;
}

.product-table .cell-img .product-img{
    width: 80px;
    height: 80px;
    border: 1px solid #ddd;
}

.product-table .cell-id{
    width: 15%;
    text-align: center;
}

.product-table .cell-price{
    width: 12.5%;
    text-align: center;
}

.product-table .cell-count{
    width: 12.5%;
    text-align: center;
    line-height: 0;
}

.product-table .cell-count .op{
    height: 20px;
    width: 20px;
    display: inline-block;
    border: solid #999;
    border-radius: 5px;
    margin: 5px;
    cursor: pointer;
}

.product-table .cell-count .count-num{
    display: inline-block;
}

.product-table .cell-info{
    width: 20%;
    text-align: left;
}

.product-table .cell-total{
    width: 15%;
    text-align: center;
}

.product-table .cell-operator{
    width: 10%;
    text-align: center;
}

/* 提交申请 */
.submit-content{
    background: #eee;
    text-align: right;
}

.submit-content .total-price{
    margin-right: 20px;
    font-size: 18px;
    font-weight: bold;
    color: #ff2525;
    vertical-align: middle;
}

.submit-content .order-submit{
    width: 100px;
    text-align: center;
    cursor: pointer;
}
</style>